/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ulatina.server;

import com.migracion.components.Column;
import com.migracion.components.Migration;
import com.migracion.dataaccess.DataAccess;
import com.migracion.dataaccess.StringInsert;
import com.migracion.pool.DataBaseConnection;
import com.migracion.pool.MSSQL;
import com.migracion.pool.MySQL;
import com.migracion.pool.Oracle;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author stevenziggiz
 */
@WebService(serviceName = "NewService")
public class NewService {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "migrarDatos")
    public Boolean migrarDatos(@WebParam(name = "migrationList") List<Migration> migrationList) {
        List<Column> sourceColumnsList;
        List<Column> destinyColumnsList;
        List<StringInsert> stringInsertList;
        int contador = 0;
        String selectColumns = "";
        String insertColumns = "";
        for (Migration migration : migrationList) {
            try {
                sourceColumnsList = migration.getSourceTable().getColumnList();
                destinyColumnsList = migration.getDestinyTable().getColumnList();
                for (Column column : destinyColumnsList) {
                    if(column.getIndexMigrar() != null){
                        contador++;
                        if(!selectColumns.equals("")){
                            selectColumns += ", ";
                        }
                        if(!insertColumns.equals("")){
                            insertColumns += ", ";
                        }
                        insertColumns += column.getField();
                        selectColumns += sourceColumnsList.get(column.getIndexMigrar()).getField();
                    }
                }
                stringInsertList = DataAccess.getDataMigration(migration.getConnectionSourceTable(), "Select "+selectColumns+" FROM "+migration.getSourceTable().getTableName(), contador);
                DataAccess.insert(migration.getConnectionDestinyTable(), stringInsertList,migration.getDestinyTable().getTableName(),insertColumns);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(NewService.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(NewService.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(NewService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "crearConnection")
    public DataBaseConnection crearConnection(@WebParam(name = "ipServer") String ipServer, @WebParam(name = "port") int port, @WebParam(name = "serviceName") String serviceName, @WebParam(name = "dataBaseName") String dataBaseName, @WebParam(name = "user") String user, @WebParam(name = "password") String password, @WebParam(name = "dataBaseType") String dataBaseType) {
        if(dataBaseType.equals("Oracle")){
            return new Oracle(ipServer, port, serviceName, dataBaseName, user, password);
        }else{
            if(dataBaseType.equals("MySQL")){
                return new MySQL(ipServer, port, dataBaseName, user, password);
            }else{
                if(dataBaseType.equals("MSSQL")){
                    return new MSSQL(ipServer, port, dataBaseName, user, password);
                }
            }
        }
        return null;
    }
}
